<?php
namespace app\admin\controller;
use think\Db;
class Product extends Base
{
    public function index(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(session ('area')){
                $map['shop_qu_id'] = ['in',session('area')];
            }
            if(isset($key)&&$key!="")
            {
                $map['p_name|shop_name'] = ['like',"%" . $key . "%"];
            }
            $map['p_status'] = ['neq',6];
            if(isset($status)&&$status != ""){
                $map['p_status'] = $status;
            }
            $map['p_type'] = ['in','2,3'];
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('product p')
                ->join('shop s','s.shop_id = p.p_shop_id','left')
                ->join('product_cate c','c.cate_id = p.p_cate','left')
                ->where($map)
                ->count();
            $lists =  Db::name('product p')
                ->join('shop s','s.shop_id = p.p_shop_id','left')
                ->join('product_cate c','c.cate_id = p.p_cate','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('p_add_time DESC,p_status asc')
                ->select ();
            if(!empty($lists)){
                foreach ($lists as $key=>$vo){
                    $lists[$key]['p_banner'] = explode (',',$vo['p_banner']);
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('goods/index');
    }

    public function cash(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(session ('area')){
                $map['shop_qu_id'] = ['in',session('area')];
            }
            if(isset($key)&&$key!="")
            {
                $map['p_name|shop_name'] = ['like',"%" . $key . "%"];
            }
            $map['p_status'] = ['neq',6];
            if(isset($status)&&$status != ""){
                $map['p_status'] = $status;
            }
            if(isset($pool)&&$pool != ""){
                $map['p_m_pool'] = $pool;
            }
            $map['p_type'] = 1;
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('product p')
                ->join('shop s','s.shop_id = p.p_shop_id','left')
                ->join('product_cate c','c.cate_id = p.p_cate','left')
                ->where($map)
                ->count();
            $lists =  Db::name('product p')
                ->join('shop s','s.shop_id = p.p_shop_id','left')
                ->join('product_cate c','c.cate_id = p.p_cate','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('p_add_time DESC,p_status asc')
                ->select ();
            $sessionUid = session ('uid');
            if(!empty($lists)){
                foreach ($lists as $key=>$vo){
                    $lists[$key]['sessionUid'] = $sessionUid;
                    $lists[$key]['p_banner'] = explode (',',$vo['p_banner']);
                }
            }
            $needPoll = Db::name('product')
                ->where('p_status',4)
                ->where('p_m_pool',2)
                ->count ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists,'needPoll'=>$needPoll]);
        }
        return $this->fetch('goods/cash');
    }

    /**
     * 申请入产品池
     * @return \think\response\Json
     */
    public function inPoll(){
        extract (input());
        $param = [
            'p_m_pool'=>2
        ];
        $res = Db::name ('product')->where('p_id',$id)->update($param);
        if($res){
            return json(['code'=>200,'msg'=>'提交成功，请耐心等待审核~']);
        }else{
            return json(['code'=>100,'msg'=>'提交失败，请刷新重试！']);
        }
    }
    /**
     * 产品池审核和编辑
     * @return \think\response\Json
     */
    public function checkPoll(){
        extract(input());
        if(request()->isPost()){
            if($type == 1){
                if($checkDo == 3){
                    $param  = [
                        'p_m_pool' => 3 ,
                        'p_m_single_all_money' => $p_m_single_all_money ,
                        'p_m_single_one_money' => $p_m_single_one_money ,
                        'p_m_single_one_jifen' => $p_m_single_one_jifen ,
                        'p_m_single_two' => $p_m_single_two ,
                        'p_m_single_system' => $p_m_single_system ,
                        'p_m_team_status' => $p_m_team_status
                    ];
                    if($p_m_team_status == 1){
                        if($p_m_team_nums < 2){
                            return json(['code'=>100,'msg'=>'开团人数不得低于2']);
                        }
                        if($p_m_team_hours < 1){
                            return json(['code'=>100,'msg'=>'有效期不能少于1小时']);
                        }
                        $param['p_m_team_all_money'] = $p_m_team_all_money;
                        $param['p_m_team_leader_money'] = $p_m_team_leader_money;
                        $param['p_m_team_leader_jifen'] = $p_m_team_leader_jifen;
                        $param['p_m_team_join_money'] = $p_m_team_join_money;
                        $param['p_m_team_join_jifen'] = $p_m_team_join_jifen;
                        $param['p_m_team_system'] = $p_m_team_system;
                        $param['p_m_team_nums'] = $p_m_team_nums;
                        $param['p_m_team_hours'] = $p_m_team_hours;
                    }
                }else{
                    $param  = [
                        'p_m_pool' => 4
                    ];
                }
            }else{
                $param  = [
                    'p_m_single_all_money' => $p_m_single_all_money ,
                    'p_m_single_one_money' => $p_m_single_one_money ,
                    'p_m_single_one_jifen' => $p_m_single_one_jifen ,
                    'p_m_single_two' => $p_m_single_two ,
                    'p_m_single_system' => $p_m_single_system ,
                    'p_m_team_status' => $p_m_team_status
                ];
                if($p_m_team_status == 1){
                    if($p_m_team_nums < 2){
                        return json(['code'=>100,'msg'=>'开团人数不得低于2']);
                    }
                    if($p_m_team_hours < 1){
                        return json(['code'=>100,'msg'=>'有效期不能少于1小时']);
                    }
                    $param['p_m_team_all_money'] = $p_m_team_all_money;
                    $param['p_m_team_leader_money'] = $p_m_team_leader_money;
                    $param['p_m_team_leader_jifen'] = $p_m_team_leader_jifen;
                    $param['p_m_team_join_money'] = $p_m_team_join_money;
                    $param['p_m_team_join_jifen'] = $p_m_team_join_jifen;
                    $param['p_m_team_system'] = $p_m_team_system;
                    $param['p_m_team_nums'] = $p_m_team_nums;
                    $param['p_m_team_hours'] = $p_m_team_hours;
                }else{
                    $have = Db::table('up_m_order')
                        ->where('mo_pay_money',2)
                        ->where('mo_status',1)
                        ->find();
                    if($have){
                        return json(['code'=>100,'msg'=>'还有进行中的团团赚，不允许关闭！']);
                    }
                }
            }

            $res = Db::table('up_product')
                ->where('p_id',$p_id)
                ->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'操作成功']);
            }else{
                return json(['code'=>100,'msg'=>'操作失败']);
            }
        }
        $order = Db::table('up_product')
            ->where('p_id',$p_id)
            ->find();
        return $this->fetch('goods/checkpoll',['data'=>$order,'type'=>$type]);
    }
    public function doCheck(){
        extract (input());
        $param = [
            'p_status'=>$type
        ];
        if($type == 4){
            $param['p_attestation_time'] = date('Y-m-d H:i:s');
        }
        $res = Db::name ('product')->where('p_id',$p_id)->update($param);
        if($res){
            return json(['code'=>200,'msg'=>'审核成功']);
        }else{
            return json(['code'=>100,'msg'=>'审核失败']);
        }
    }


    public function soldOut(){
        extract (input());
        $res = Db::name ('product')->where('p_id',$id)->setField ('p_status',5);
        if($res){
            return json(['code'=>200,'msg'=>'下架成功']);
        }else{
            return json(['code'=>100,'msg'=>'下架失败']);
        }
    }


    public function seeModel(){
        extract (input ());
        if(request()->isAjax ()){
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['m_name'] = ['like',"%" . $key . "%"];
            }
            $map['m_product_id'] = session('p_id');
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('product_model')
                ->where($map)
                ->count();
            $lists =  Db::name('product_model')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('m_add_time')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        session('p_id',$id);
        return $this->fetch('goods/model');
    }


    public function goodsStatus(){
        extract (input());
        if($type == 1){
            $name = 'p_today_th';
        }elseif($type == 2){
            $name = 'p_city_th';
        }elseif($type == 3){
            $name = 'p_man_tj';
        }else{
            $name = 'p_hot';
        }
        $res = Db::name('product')->where('p_id',$id)->setField ($name,$num);
        if(!$res){
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    public function goodsStatus1(){
        extract (input());
        if($type == 1){
            $name = 'p_city_th_zong';
        }elseif ($type == 2) {
            $name = 'p_man_tj_zong';
        }elseif ($type == 3) {
            $name = 'p_search';
        }else{
            $name = 'p_you_tj';
        }
        $res = Db::name('product')->where('p_id',$id)->setField ($name,$num);
        if(!$res){
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    // 现金商品状态
    public function goodsStatus2(){
        extract (input());
        if($type == 1){
            $name = 'p_admin_tj';
        }elseif($type == 2){
            $name = 'p_head_tj';
        }elseif ($type == 3){
            $name = 'p_you_tj';
        }elseif($type == 4){
            $name = 'p_search';
        }elseif($type == 5){
            $name = 'p_admin_tj_zong';
        }
        $res = Db::name('product')->where('p_id',$id)->setField ($name,$num);
        if(!$res){
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    public function addSort(){
        extract (input());
        $res = Db::name('product')
            ->where('p_id',$id)
            ->update(['p_sort'=>$num,'p_edit_time'=>date('Y-m-d H:i:s')]);
        if($res){
            return json(['code'=>200,'msg'=>'编辑成功']);
        }else{
            return json(['code'=>100,'msg'=>'编辑失败']);
        }
    }

    public function updateSell(){
        extract (input());
        $res = Db::name('product')
            ->where('p_id',$id)
            ->update(['p_sell_yet'=>$num,'p_edit_time'=>date('Y-m-d H:i:s')]);
        if($res){
            return json(['code'=>200,'msg'=>'编辑成功']);
        }else{
            return json(['code'=>100,'msg'=>'编辑失败']);
        }
    }

}